package com.ruoyi.system.mapper;


import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface StudentMapper {

    /**
     * 根据id修改手机号信息(手机认证)
     * @param student
     * @return
     */
    public void updateStudnetPhoneNumById(Student student);

    @Insert("insert into student values(#{sid},#{schooName},#{className},#{epId},#{createtime},#{nickname},#{operatorId},#{directTeacher},#{realname},#{username},#{phoneNum},#{qq},#{wx},#{email},#{source},#{teacherId},#{sfz},#{status},#{groupId},#{event_stage})")
    Integer saveStudent(Student student);

    @Select("select count(*) from student where epId =#{epId} and sid = #{eid}")
    Integer selectStudentByEidAndEpId(@Param("eid") String eid, @Param("epId") String epId);


    List<SyseEventsPosted> selectEventsPostedAndAggregation(Map<String, Object> param);

    List<Student> selectEventsPostedForStudents(Student student);

    @Select("select * from student where epId = #{epid} order by createtime desc")
    List<Student> selectByEpIdAndEpstate(String epid);

    @Select("select * from student where epId =#{epId} and sid = #{sid}")
    Student selectStudentBySidAndEpId(@Param("sid") String sid, @Param("epId") String epId);

    /**
     * 根据大赛id查询参与该大赛的所有学生信息
     */
    public List<Student> queryStudentsByEpid(Integer epid);

    /**
     * 报名审批
     * @param sid  学生ID
     * @param epId  赛事ID
     * @param status  审批状态
     * @return 响应行数
     */
    @Update("update student set status = #{status} where sid = #{sid} and epId = #{epId}")
    Integer baoMingShenPi(@Param("sid") String sid,@Param("epId") String epId,@Param("status") int status);

    /**
     * 修改报名学生的晋级状态
     * @param sid 学生ID
     * @param epId  赛事ID
     * @param eventStage  晋级阶段
     * @return
     */
    @Update("update student set event_stage = #{eventStage} where sid = #{sid} and epId = #{epId}")
    Integer upgradeStudent(@Param("sid") String sid,@Param("epId") String epId,@Param("eventStage") String eventStage);

    /**
     * 删除已经报名赛事得学生
     * */
    @Delete("delete from student where sid = #{sid} and epId = #{epId}")
    Integer deleteStudentBySid(@Param("sid") String sid,@Param("epId") String epId);

    /**
     * 根据学生ID查询所报名的赛事
     * @param userId
     * @return
     */
    @Select("SELECT s.* , e.eptitle, e.progressStatus, e.epstate, e.epworks, e.epprestart, e.eppreend, e.epsecstart, e.epsecend, e.epfinalstart, e.epfinalend, e.eppreexpired, e.epsecexpired, e.epfinalexpired, \n" +
            "case when c.id IS NULL then '未提交' ELSE '已提交' END AS cmtStatus, d.dict_label groupName \n" +
            "from student s INNER JOIN events_posted e on s.epId = e.epid \n" +
            "left join work c ON s.sid = c.sid AND e.epid = c.epid AND s.event_stage = c.scoring_step\n" +
            "LEFT JOIN sys_dict_data d ON s.group_id = d.dict_value \n" +
            "WHERE s.sid = #{userId}")
    List<Map<String, Object>> selectEventsByUserID(String userId);

    /**
     * 根据老师ID查询所报名的赛事
     * @param teacherId
     * @return
     */
    @Select("SELECT s.* , e.eptitle, case when c.id IS NULL then '未提交' ELSE '已提交' END AS cmtStatus\n" +
            "from student s INNER JOIN events_posted e on s.epId = e.epid \n" +
            "left join work c ON s.sid = c.sid AND e.epid = c.epid AND s.event_stage = c.scoring_step\n" +
            "WHERE s.teacherId = #{teacherId}")
    List<Map<String, Object>> selectEventsByTeacherID(String teacherId);

    /**
     *
     * */
    List<Student> selectEventsAndAggreTeacher(Student student);
}
